#import "VLayoutView.h"

@implementation VLayoutView

- (id)initWithSpacing:(int)s
{
    if (self = [super init])
    {
        spacing = s;
    }
    return self;
}

- (CGSize)layoutSubviewsEffectively:(BOOL)effectively
{
    int y = 0, max_x = 0;
    for (UIView *child in self.subviews)
    {
        if (y != 0)
            y += spacing;
        if (effectively)
            child.frame = CGRectMake(0, y, child.frame.size.width,
                                     child.frame.size.height);
        y += child.frame.size.height;
        if (max_x < child.frame.size.width)
            max_x = child.frame.size.width;
    }
    return CGSizeMake(max_x, y);
}

- (void)layoutSubviews
{
    [self layoutSubviewsEffectively:YES];
}

- (CGSize)sizeThatFits:(CGSize)size
{
    return [self layoutSubviewsEffectively:NO];
}

@end
